using UnityEngine;
using System.Collections;
using Holoville.HOTween;

public class PinchZoom : MonoBehaviour
{
	public float screenHdefault = 600;
	public float screenWdefault = 1024;
	public GameObject goZoom;
	[HideInInspector]
	public GameObject goPic;
	public UISprite pic;

	public static int indexTouch = 0;
	private Vector2 curPosTouch1 = Vector2.zero, lastPosTouch1 = Vector2.zero, curPosTouch2 = Vector2.zero, lastPosTouch2 = Vector2.zero;
	private float curDist = 0.0f, lastDist = 0.0f;
	[HideInInspector]
	public float zoomFactor = 2;
	float minZoom = 1;
	float minSuperZoom = 1;
	float maxZoom = 1;
	float maxSuperZoom = 1;

	Tweener tweenerMin = null;
	Tweener tweenerMax = null;

	void Start()
	{
		goPic = pic.gameObject;
		RecheckMaxMin();
	}

	private void RecheckMaxMin()
	{
		float scaleH = screenHdefault / (float)pic.atlas.spriteList[0].height;
		float scaleW = screenWdefault / (float)pic.atlas.spriteList[0].width;
		if (scaleH > scaleW)
		{
			minZoom = scaleW;
		}
		else
		{
			minZoom = scaleH;
		}
		minSuperZoom = 90f * minZoom / 100f;
		maxZoom = 1.3f;
		maxSuperZoom = 110f * maxZoom / 100f;
	}


	Vector3 scaleV = new Vector3();
	void PicClick()
	{
		//if (goZoom.transform.localScale == Vector3.one)
		//{
		//    float scaleH = screenHdefault / (float)pic.atlas.spriteList[0].height;
		//    float scaleW = screenWdefault / (float)pic.atlas.spriteList[0].width;
		//    if (scaleH > scaleW)
		//    {
		//        scaleV.x = scaleW;
		//    }
		//    else
		//    {
		//        scaleV.x = scaleH;
		//    }
		//    scaleV.y = scaleV.x;
		//    scaleV.z = scaleV.x;
		//    goZoom.transform.localScale = scaleV;
		//}
		//else
		//{
		//    goZoom.transform.localScale = Vector3.one;
		//}
	}

	void Update()
	{
		if (Input.touchCount > 1 && getVCenPic)
		{
			vCenPic = pic.gameObject.transform.position;
			getVCenPic = false;
		}
		if (Input.touchCount < 2)
		{
			getVCenPic = true;
			getVCenTou = true;
		}
		if (Input.touches.Length <= 0)
		{
			if (zoomFactor > maxZoom && (tweenerMax == null || tweenerMax.isComplete))
			{
				tweenerMax = HOTween.To(this, 0.250f, new TweenParms().Prop("zoomFactor", maxZoom).Ease(EaseType.Linear).OnUpdate(UpdateZoom));				
			}
			if (zoomFactor < minZoom && (tweenerMin == null || tweenerMin.isComplete))
			{
				tweenerMin = HOTween.To(this, 0.250f, new TweenParms().Prop("zoomFactor", minZoom).Ease(EaseType.Linear).OnUpdate(UpdateZoom));
			}
		}
		else
		{
			
			for (int i = 0; i < Input.touchCount; i++)
			{
				indexTouch = i;
				switch (Input.GetTouch(i).phase)
				{
					case TouchPhase.Moved:
					case TouchPhase.Stationary:
						Zoom();
						break;
				}
			}
		}
	}

	void UpdateZoom()
	{
		scaleV.x = zoomFactor;
		scaleV.y = zoomFactor;
		scaleV.z = zoomFactor;
		goZoom.transform.localScale = scaleV;
		pic.gameObject.transform.position = vCenPic + posAdd;
	}
	bool getVCenPic = true;
	bool getVCenTou = true;
	Vector3 vCenPic;
	Vector3 vCenTou;
	Vector3 posAdd;
	void Zoom()
	{
		switch (indexTouch)
		{
			case 0://first touch
				curPosTouch1 = Input.GetTouch(0).position;
				lastPosTouch1 = curPosTouch1 - Input.GetTouch(0).deltaPosition;
				goZoom.transform.localPosition += new Vector3(Input.GetTouch(0).deltaPosition.x, Input.GetTouch(0).deltaPosition.y, 0);
				break;
			case 1://second touch
				curPosTouch2 = Input.GetTouch(1).position;
				lastPosTouch2 = curPosTouch2 - Input.GetTouch(1).deltaPosition;
				break;
		}

		//finds the distance between your moved touches
		//we dont want to find the distance between 1 finger and nothing
		if (indexTouch >= 1)
		{
			curDist = Vector2.Distance(curPosTouch1, curPosTouch2);
			lastDist = Vector2.Distance(lastPosTouch1, lastPosTouch2);
		}
		else
		{
			curDist = 0.0f;
			lastDist = 0.0f;
		}

		//Calculate the zoom magnitude
		if ((int)(lastDist*1000) == 0)
		{
			return;
		}
		zoomFactor += (curDist / lastDist) - 1;
		if (tweenerMin != null && tweenerMin.isComplete == false)
		{
			tweenerMin.Complete();
		}
		if (tweenerMax != null && tweenerMax.isComplete == false)
		{
			tweenerMax.Complete();
		}
		if (getVCenTou)
		{
			vCenTou = (curPosTouch1 + curPosTouch2) * 0.5f;
			getVCenTou = false;
		}
		posAdd = (curDist / lastDist -1) * (vCenTou - vCenPic);
		UpdateZoom();
	}
}